home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  2.5 KB  |  103 lines

  1. /*  expand - expand tabs to spaces      Author: Terrence W. Holm */
  2.  
  3. /*  Usage:  expand  [ -tab1,tab2,tab3,... ]  [ file ... ]  */
  4.  
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #define MAX_TABS 32
  11.  
  12. int column = 0;                 /* Current column, retained between files  */
  13.  
  14. int main  (int argc, char **argv);
  15. void Expand  (FILE *f, int tab_index, int tabs []);
  16.  
  17. int main(argc, argv)
  18. /* [<][>][^][v][top][bottom][index][help] */
  19. int argc;
  20. char *argv[];
  21. {
  22.   int tabs[MAX_TABS];
  23.   int tab_index = 0;            /* Default one tab   */
  24.   int i;
  25.   FILE *f;
  26.  
  27.   tabs[0] = 8;                  /* Default tab stop  */
  28.  
  29.   if (argc > 1 && argv[1][0] == '-') {
  30.         char *p = argv[1];
  31.         int last_tab_stop = 0;
  32.  
  33.         for (tab_index = 0; tab_index < MAX_TABS; ++tab_index) {
  34.                 if ((tabs[tab_index] = atoi(p + 1)) <= last_tab_stop) {
  35.                         fprintf(stderr, "Bad tab stop spec\n");
  36.                         exit(1);
  37.                 }
  38.                 last_tab_stop = tabs[tab_index];
  39.  
  40.                 if ((p = strchr(p + 1, ',')) == NULL) break;
  41.         }
  42.  
  43.         --argc;
  44.         ++argv;
  45.   }
  46.   if (argc == 1)
  47.         Expand(stdin, tab_index, tabs);
  48.   else
  49.         for (i = 1; i < argc; ++i) {
  50.                 if ((f = fopen(argv[i], "r")) == NULL) {
  51.                         perror(argv[i]);
  52.                         exit(1);
  53.                 }
  54.                 Expand(f, tab_index, tabs);
  55.                 fclose(f);
  56.         }
  57.  
  58.   return(0);
  59. }
  60.  
  61.  
  62. void Expand(f, tab_index, tabs)
  63. /* [<][>][^][v][top][bottom][index][help] */
  64. FILE *f;
  65. int tab_index;
  66. int tabs[];
  67. {
  68.   int next;
  69.   int c;
  70.   int i;
  71.  
  72.   while ((c = getc(f)) != EOF) {
  73.         if (c == '\t') {
  74.                 if (tab_index == 0)
  75.                         next = (column / tabs[0] + 1) * tabs[0];
  76.                 else {
  77.                         for (i = 0; i <= tab_index && tabs[i] <= column; ++i);
  78.  
  79.                         if (i > tab_index)
  80.                                 next = column + 1;
  81.                         else
  82.                                 next = tabs[i];
  83.                 }
  84.  
  85.                 do {
  86.                         ++column;
  87.                         putchar(' ');
  88.                 } while (column < next);
  89.  
  90.                 continue;
  91.         }
  92.         if (c == '\b')
  93.                 column = column > 0 ? column - 1 : 0;
  94.         else if (c == '\n' || c == '\r')
  95.                 column = 0;
  96.         else
  97.                 ++column;
  98.  
  99.         putchar(c);
  100.   }
  101. }
  102. /* [<][>][^][v][top][bottom][index][help] */
  103.